import { Page } from '@/base/Page'
import { deepAssign } from '@/base/util/Objects';
import { ModelSelfMotion } from '@/base/model/ModelSelfMotion';
import { DeviceSort } from '@/base/model/DeviceSort';
import { DeviceModel } from '@/base/model/DeviceModel';
import {
  deleteModelSelfMotionId,
  getModelSelfMotion,
  getModelSelfMotionId,
  postModelSelfMotion,
  PutModelSelfMotion,
} from '@/base/api/ModelSelfMotionApi'
import { getDeviceModel, getDeviceModelId } from '@/base/api/DeviceModelApi'
import { getDeviceSort } from '@/base/api/DeviceSortApi'

export class AutomaticBloc {

  page: Page<DeviceSort> = new Page<DeviceSort>([], 0, 1, 15, 0, []);
  page1: Page<DeviceModel> = new Page<DeviceModel>([], 0, 1, 15, 0, []);
  modelSelfMotion: Page<ModelSelfMotion> = new Page<ModelSelfMotion>([], 0, 1, 10, 0, []);
  model: any = {
    id: 0,
    selfMotion: '',
    selfMotionEnglish: '',
    selfMotionId: 0,
    deviceModelId: 0,
    created: '',
    updated: '',
  };
  modelSelfMotionId: ModelSelfMotion = {
    id: 0,
    selfMotion: '',
    selfMotionEnglish: '',
    selfMotionId: 0,
    deviceModelId: 0,
    created: '',
    updated: '',
  }
  sortList: any = [];
  modelList: any = [];
  agentList: any = [];
  versions: any = [];
  fileList: any = [];
  fileSelectIcon: any = [];
  fileListBackground: any = [];
  appList: any=[];
  initContent = '';
  getAllSort(pageIndex: number, pageSize: number, sortDirection: string) {
    let that = this;
    return getDeviceSort({
      pageIndex,
      pageSize,
      sortDirection
    }).then((res) => {
      deepAssign(this.page, res.data);
      this.sortList = this.page.items.map(function (item) {
        var obj = { id: 0, sortName: '' };
        // @ts-ignore
        obj.id = item.id;
        obj.sortName = item.sortName;
        return obj
      });
      return res;
    })
  }

  getAllModel(pageIndex: number, pageSize: number, sortId: number, sortDirection: string, companyId) {
    let that = this;
    return getDeviceModel({
      pageIndex,
      pageSize,
      sortId,
      sortDirection,
      companyId,
      id: this.model.id,
      modelName: this.model.modeName
    }).then((res) => {
      deepAssign(this.page1, res.data);
      this.modelList = this.page1.items.map(function (item) {
        var obj = { id: 0, modelName: '' };
        obj.id = item.id;
        obj.modelName = item.modelName;
        return obj
      });
      return res;
    })
  }

  getModel(id: number) {
    return getModelSelfMotionId( id ).then((res) => {
      return res;
    })
  }

  addSeftMotion(
    selfMotion: string,
    selfMotionEnglish: string,
    selfMotionId: number,
    deviceModelId: number,) {
    return postModelSelfMotion({
      selfMotion,
      selfMotionEnglish,
      selfMotionId,
      deviceModelId
    }).then((res) => {
      this.getModelSelfMotion(this.modelSelfMotion.pageIndex, 10, 'DESC', this.model
      )
      return res;
    }).finally(() => {
    })
  }

  getModelSelfMotion(
    pageIndex: number, pageSize: number, sortDirection: string, model: any) {
    return getModelSelfMotion({
      pageIndex: pageIndex,
      pageSize: pageSize,
      sortDirection: sortDirection,
      selfMotion: model.selfMotion,
      selfMotionEnglish: model.selfMotionEnglish,
      deviceModelId: model.deviceModelId,
    }).then((res) => {
      console.log(res.data);
      deepAssign(this.modelSelfMotion, res.data);
      return res;
    })
  }


  toModifyItem(id: number) {
    return getModelSelfMotionId(
      id
    ).then((res) => {
      deepAssign(this.modelSelfMotionId, res.data);
      return res;
    })
  }

  modifyItem(
    id: number,
    selfMotion: string,
    selfMotionEnglish: string,
    selfMotionId: number,
    deviceModelId: number) {
    return PutModelSelfMotion({
      id,
      selfMotion,
      selfMotionEnglish,
      selfMotionId,
      deviceModelId,
    }).then((res) => {
      deepAssign(this.modelSelfMotionId, res.data);
      this.getModelSelfMotion(this.modelSelfMotion.pageIndex, 10, 'DESC', this.model)
      return res;
    })
  }

  removeItem(dateItem: ModelSelfMotion) {
    return deleteModelSelfMotionId(dateItem.id)
      .then((res) => {
        this.getModelSelfMotion(this.modelSelfMotion.pageIndex, 10, 'DESC', this.model)
        return res;
      })
  }

}
